package com.sukaiyi.generator.actions;

import com.sukaiyi.generator.actions.model.ActionContext;
import com.intellij.ide.IdeView;
import com.intellij.openapi.actionSystem.*;
import com.intellij.openapi.project.Project;
import com.intellij.psi.PsiDirectory;
import com.intellij.psi.PsiFile;
import org.jetbrains.annotations.NotNull;

/**
 * @author sukaiyi
 */
public abstract class BaseHtAction extends AnAction {

	@Override
	public void actionPerformed(@NotNull AnActionEvent event) {
		DataContext dataContext = event.getDataContext();
		IdeView view = LangDataKeys.IDE_VIEW.getData(dataContext);
		if (view == null) {
			return;
		}
		Project project = PlatformDataKeys.PROJECT.getData(dataContext);
		if (project == null) {
			return;
		}
		PsiDirectory dir = view.getOrChooseDirectory();
		if (dir == null) {
			return;
		}
		PsiFile file = event.getData(LangDataKeys.PSI_FILE);

		ActionContext context = new ActionContext();
		context.setDataContext(dataContext);
		context.setProject(project);
		context.setFile(file);
		context.setDirectory(dir);
		process(context);
	}

	/**
	 * 处理点击事件
	 * @param context 点击事件发生时的参数
	 */
	protected abstract void process(ActionContext context);
}
